home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1865 / 1865.xpi / chrome / adblockplus.jar / content / subscriptionClasses.js < prev    next >
Text File  |  2010-01-07  |  10KB  |  423 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Adblock Plus.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Wladimir Palant.
  18.  * Portions created by the Initial Developer are Copyright (C) 2006-2009
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *
  23.  * ***** END LICENSE BLOCK ***** */
  24.  
  25. /**
  26.  * @fileOverview Definition of Subscription class and its subclasses.
  27.  * This file is included from AdblockPlus.js.
  28.  */
  29.  
  30. /**
  31.  * Abstract base class for filter subscriptions
  32.  *
  33.  * @param {String} url    download location of the subscription
  34.  * @constructor
  35.  */
  36. function Subscription(url)
  37. {
  38.     this.url = url;
  39.     this.filters = [];
  40.     Subscription.knownSubscriptions[url] = this;
  41. }
  42. Subscription.prototype =
  43. {
  44.     /**
  45.      * Download location of the subscription
  46.      * @type String
  47.      */
  48.     url: null,
  49.  
  50.     /**
  51.      * Filters contained in the filter subscription
  52.      * @type Array of Filter
  53.      */
  54.     filters: null,
  55.  
  56.     /**
  57.      * Defines whether the filters in the subscription should be disabled
  58.      * @type Boolean
  59.      */
  60.     disabled: false,
  61.  
  62.     /**
  63.      * Serializes the filter to an array of strings for writing out on the disk.
  64.      * @param {Array of String} buffer  buffer to push the serialization results into
  65.      */
  66.     serialize: function(buffer)
  67.     {
  68.         buffer.push("[Subscription]");
  69.         buffer.push("url=" + this.url);
  70.         if (this.disabled)
  71.             buffer.push("disabled=true");
  72.     },
  73.  
  74.     serializeFilters: function(buffer)
  75.     {
  76.         for each (let filter in this.filters)
  77.             buffer.push(filter.text.replace(/\[/g, "\\["));
  78.     },
  79.  
  80.     toString: function()
  81.     {
  82.         let buffer = [];
  83.         this.serialize(buffer);
  84.         return buffer.join("\n");
  85.     }
  86. };
  87. abp.Subscription = Subscription;
  88.  
  89. /**
  90.  * Cache for known filter subscriptions, maps URL to subscription objects.
  91.  * @type Object
  92.  */
  93. Subscription.knownSubscriptions = {__proto__: null};
  94.  
  95. /**
  96.  * Returns a subscription from its URL, creates a new one if necessary.
  97.  * @param {String} url  URL of the subscription
  98.  * @return {Subscription} subscription or null if the subscription couldn't be created
  99.  */
  100. Subscription.fromURL = function(url)
  101. {
  102.     if (url in Subscription.knownSubscriptions)
  103.         return Subscription.knownSubscriptions[url];
  104.  
  105.     if (url in SpecialSubscription.map && SpecialSubscription.map[url] instanceof Array)
  106.         return new SpecialSubscription(url);
  107.     else
  108.     {
  109.         try
  110.         {
  111.             // Test URL for validity
  112.             url = ioService.newURI(url, null, null).spec;
  113.             return new DownloadableSubscription(url, null);
  114.         }
  115.         catch (e)
  116.         {
  117.             return null;
  118.         }
  119.     }
  120. }
  121.  
  122. /**
  123.  * Deserializes a subscription
  124.  *
  125.  * @param {Object}  obj map of serialized properties and their values
  126.  * @return {Subscription} subscription or null if the subscription couldn't be created
  127.  */
  128. Subscription.fromObject = function(obj)
  129. {
  130.     let result;
  131.     if (obj.url in SpecialSubscription.map && SpecialSubscription.map[obj.url] instanceof Array)
  132.         result = new SpecialSubscription(obj.url);
  133.     else
  134.     {
  135.         if ("external" in obj && obj.external == "true")
  136.             result = new ExternalSubscription(obj.url, obj.title);
  137.         else
  138.         {
  139.             try
  140.             {
  141.                 // Test URL for validity
  142.                 obj.url = ioService.newURI(obj.url, null, null).spec;
  143.             }
  144.             catch (e)
  145.             {
  146.                 return null;
  147.             }
  148.  
  149.             result = new DownloadableSubscription(obj.url, obj.title);
  150.             if ("autoDownload" in obj)
  151.                 result.autoDownload = (obj.autoDownload == "true");
  152.             if ("nextURL" in obj)
  153.                 result.nextURL = obj.nextURL;
  154.             if ("downloadStatus" in obj)
  155.                 result.downloadStatus = obj.downloadStatus;
  156.             if ("lastModified" in obj)
  157.                 result.lastModified = obj.lastModified;
  158.             if ("expires" in obj)
  159.                 result.expires = parseInt(obj.expires) || 0;
  160.             if ("errors" in obj)
  161.                 result.errors = parseInt(obj.errors) || 0;
  162.             if ("requiredVersion" in obj)
  163.             {
  164.                 result.requiredVersion = obj.requiredVersion;
  165.                 if (abp.versionComparator.compare(result.requiredVersion, abp.getInstalledVersion()) > 0)
  166.                     result.upgradeRequired = true;
  167.             }
  168.             if ("alternativeLocations" in obj)
  169.                 result.alternativeLocations = obj.alternativeLocations;
  170.         }
  171.         if ("lastDownload" in obj)
  172.             result.lastDownload = parseInt(obj.lastDownload) || 0;
  173.     }
  174.     if ("disabled" in obj)
  175.         result.disabled = (obj.disabled == "true");
  176.  
  177.     return result;
  178. }
  179.  
  180. /**
  181.  * Class for special filter subscriptions (user's filters)
  182.  * @param {String} url see Subscription()
  183.  * @constructor
  184.  * @augments Subscription
  185.  */
  186. function SpecialSubscription(url)
  187. {
  188.     Subscription.call(this, url);
  189.  
  190.     let data = SpecialSubscription.map[url];
  191.     this._titleID = data[0];
  192.     this._priority = data[1];
  193.     this.filterTypes = data.slice(2);
  194. }
  195. SpecialSubscription.prototype =
  196. {
  197.     __proto__: Subscription.prototype,
  198.  
  199.     /**
  200.      * ID of the string that should be used as the title of this subscription
  201.      * @type String
  202.      */
  203.     _titleID: null,
  204.  
  205.     /**
  206.      * Priority when adding new filters that are accepted by multiple subscriptions
  207.      * @type Integer
  208.      */
  209.     _priority: null,
  210.  
  211.     /**
  212.      * Priority based on which new filters are added to a subscription if multiple
  213.      * subscriptions are possible
  214.      * @type Integer
  215.      */
  216.     get priority()
  217.     {
  218.         return this._priority;
  219.     },
  220.  
  221.     /**
  222.      * Title of the subscription (read-only)
  223.      * @type String
  224.      */
  225.     get title()
  226.     {
  227.         return abp.getString(this._titleID);
  228.     },
  229.  
  230.     /**
  231.      * Filter classes that can be added to this subscription
  232.      * @type Array of Function
  233.      */
  234.     filterTypes: null,
  235.  
  236.     /**
  237.      * Tests whether a filter is allowed to be added to this subscription
  238.      * @param {Filter} filter filter to be tested
  239.      * @return {Boolean}
  240.      */
  241.     isFilterAllowed: function(filter)
  242.     {
  243.         for each (let type in this.filterTypes)
  244.             if (filter instanceof type)
  245.                 return true;
  246.  
  247.         return false;
  248.     }
  249. };
  250. abp.SpecialSubscription = SpecialSubscription;
  251.  
  252. SpecialSubscription.map = {
  253.     __proto__: null,
  254.     "~il~": ["invalid_description", 1, InvalidFilter, CommentFilter],
  255.     "~wl~": ["whitelist_description", 3, WhitelistFilter, CommentFilter],
  256.     "~fl~": ["filterlist_description", 4, BlockingFilter, CommentFilter],
  257.     "~eh~": ["elemhide_description", 2, ElemHideFilter, CommentFilter]
  258. };
  259.  
  260. /**
  261.  * Abstract base class for regular filter subscriptions (both internally and externally updated)
  262.  * @param {String} url    see Subscription()
  263.  * @param {String} title  (optional) title of the filter subscription
  264.  * @constructor
  265.  * @augments Subscription
  266.  */
  267. function RegularSubscription(url, title)
  268. {
  269.     Subscription.call(this, url);
  270.  
  271.     this.title = title || url;
  272. }
  273. RegularSubscription.prototype =
  274. {
  275.     __proto__: Subscription.prototype,
  276.  
  277.     /**
  278.      * Title of the filter subscription
  279.      * @type String
  280.      */
  281.     title: null,
  282.  
  283.     /**
  284.      * Time of the last subscription download (in milliseconds since the beginning of the epoch)
  285.      * @type Number
  286.      */
  287.     lastDownload: 0,
  288.  
  289.     /**
  290.      * See Subscription.serialize()
  291.      */
  292.     serialize: function(buffer)
  293.     {
  294.         Subscription.prototype.serialize.call(this, buffer);
  295.         buffer.push("title=" + this.title);
  296.         if (this.lastDownload)
  297.             buffer.push("lastDownload=" + this.lastDownload);
  298.     }
  299. };
  300. abp.RegularSubscription = RegularSubscription;
  301.  
  302. /**
  303.  * Class for filter subscriptions updated by externally (by other extension)
  304.  * @param {String} url    see Subscription()
  305.  * @param {String} title  see RegularSubscription()
  306.  * @constructor
  307.  * @augments RegularSubscription
  308.  */
  309. function ExternalSubscription(url, title)
  310. {
  311.     RegularSubscription.call(this, url, title);
  312. }
  313. ExternalSubscription.prototype =
  314. {
  315.     __proto__: RegularSubscription.prototype,
  316.  
  317.     /**
  318.      * See Subscription.serialize()
  319.      */
  320.     serialize: function(buffer)
  321.     {
  322.         RegularSubscription.prototype.serialize.call(this, buffer);
  323.         buffer.push("external=true");
  324.     }
  325. };
  326. abp.ExternalSubscription = ExternalSubscription;
  327.  
  328. /**
  329.  * Class for filter subscriptions updated by externally (by other extension)
  330.  * @param {String} url    see Subscription()
  331.  * @param {String} title  see RegularSubscription()
  332.  * @constructor
  333.  * @augments RegularSubscription
  334.  */
  335. function DownloadableSubscription(url, title)
  336. {
  337.     RegularSubscription.call(this, url, title);
  338. }
  339. DownloadableSubscription.prototype =
  340. {
  341.     __proto__: RegularSubscription.prototype,
  342.  
  343.     /**
  344.      * Defines whether the subscription should be downloaded automatically
  345.      * @type Boolean
  346.      */
  347.     autoDownload: true,
  348.  
  349.     /**
  350.      * Next URL the downloaded should be attempted from (in case of redirects)
  351.      * @type String
  352.      */
  353.     nextURL: null,
  354.  
  355.     /**
  356.      * Status of the last download (ID of a string)
  357.      * @type String
  358.      */
  359.     downloadStatus: null,
  360.  
  361.     /**
  362.      * Value of the Last-Modified header returned by the server on last download
  363.      * @type String
  364.      */
  365.     lastModified: null,
  366.  
  367.     /**
  368.      * Expiration time of the filter subscription (in milliseconds since the beginning of the epoch)
  369.      * @type Number
  370.      */
  371.     expires: 0,
  372.  
  373.     /**
  374.      * Number of download failures since last success
  375.      * @type Number
  376.      */
  377.     errors: 0,
  378.  
  379.     /**
  380.      * Minimal Adblock Plus version required for this subscription
  381.      * @type String
  382.      */
  383.     requiredVersion: null,
  384.  
  385.     /**
  386.      * Should be true if requiredVersion is higher than current Adblock Plus version
  387.      * @type Boolean
  388.      */
  389.     upgradeRequired: false,
  390.  
  391.     /**
  392.      * Value of the X-Alternative-Locations header: comma-separated list of URLs
  393.      * with their weighting factors, e.g.: http://foo.example.com/;q=0.5,http://bar.example.com/;q=2
  394.      * @type String
  395.      */
  396.     alternativeLocations: null,
  397.  
  398.     /**
  399.      * See Subscription.serialize()
  400.      */
  401.     serialize: function(buffer)
  402.     {
  403.         RegularSubscription.prototype.serialize.call(this, buffer);
  404.         if (!this.autoDownload)
  405.             buffer.push("autoDownload=false");
  406.         if (this.nextURL)
  407.             buffer.push("nextURL=" + this.nextURL);
  408.         if (this.downloadStatus)
  409.             buffer.push("downloadStatus=" + this.downloadStatus);
  410.         if (this.lastModified)
  411.             buffer.push("lastModified=" + this.lastModified);
  412.         if (this.expires)
  413.             buffer.push("expires=" + this.expires);
  414.         if (this.errors)
  415.             buffer.push("errors=" + this.errors);
  416.         if (this.requiredVersion)
  417.             buffer.push("requiredVersion=" + this.requiredVersion);
  418.         if (this.alternativeLocations)
  419.             buffer.push("alternativeLocations=" + this.alternativeLocations);
  420.     }
  421. };
  422. abp.DownloadableSubscription = DownloadableSubscription;
  423.